﻿using Autodesk.AutoCAD.DatabaseServices;
using JsonYNCNumsys.entity;
using System.Collections.Generic;
using ACADDatabase = Autodesk.AutoCAD.DatabaseServices.Database;

namespace JsonYNCNumsys
{
    /// <summary>
    /// 获取标题栏。
    /// </summary>
    public class GetTitleDal
    {
        #region 属性
        /// <summary>
        /// 块引用.
        /// </summary>
        BlockReference m_brf;
        /// <summary>
        /// 事务,为空表示操作当前图纸空间.
        /// </summary>
        private Transaction m_transaction;
        /// <summary>
        /// 图纸文档WorkingDatabase,为空表示当前的图纸空间.
        /// </summary>
        private ACADDatabase m_dataBase;
        /// <summary>
        /// 该值如果为True,则(1)m_transaction,m_dataBase;(2)事务提交在该类内完成.
        /// </summary>
        private bool m_TransactionIsUsed;
        /// <summary>
        /// 当前块引用对象的属性值集合;
        /// </summary>
        private List<BlockAttributeEntity> m_lstAttr;

        public List<BlockAttributeEntity> LstAttr
        {
            get { return m_lstAttr; }
        }
        #endregion

        public GetTitleDal(ACADDatabase wDataBase,
            Transaction trsTransaction, 
            BlockReference brfBlockReference)
        {

            m_dataBase = wDataBase;
            m_transaction = trsTransaction;
            m_brf = brfBlockReference;
            m_TransactionIsUsed = true;
            m_lstAttr = new List<BlockAttributeEntity>();
        }

        public GetTitleDal(BlockReference brfBlockReference)
        {

            m_dataBase = HostApplicationServices.WorkingDatabase;
            m_transaction = null;
            m_brf = brfBlockReference;
            m_TransactionIsUsed = true;
            m_lstAttr = new List<BlockAttributeEntity>();
        }

        private void DoGetAttributeValue()
        {
            foreach (ObjectId attId in m_brf.AttributeCollection)
            {
                AttributeReference attRef = (AttributeReference)m_transaction.GetObject(attId, OpenMode.ForWrite, false);
                if (attRef.Tag != "")
                {
                    m_lstAttr.Add(new BlockAttributeEntity(attRef.Tag, attRef.TextString));
                }
            }
        }

        public void Do()
        {
            m_lstAttr.Clear();
            if (!m_TransactionIsUsed)
            {
                using (m_transaction = m_dataBase.TransactionManager.StartTransaction())
                {
                    DoGetAttributeValue();
                }
            }
            else
            {
                DoGetAttributeValue();
            }
        }
        /// <summary>
        /// 该函数必须在执行操作之后进行.
        /// </summary>
        /// <param name="str_属性名"></param>
        /// <returns></returns>
        public string GetAttributeTextString(string sTag)
        {
            string strTextString = null;
            foreach (BlockAttributeEntity att in m_lstAttr)
            {
                if (att.Tag == sTag)
                {
                    strTextString = att.TextString;
                    break;
                }
            }
            return strTextString;
        }
    }
}
